const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navId:"",//导航id
    page:1,
    size:8, //每页条数
    pendingOrdersData:[], //待付款数据
    footer_more:"", //无数据
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0, //重试或下拉是0，上啦是1
    roleCode:"",//身份 1是分总 2是员工 3是财务
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRecordData();

    this.getLevelData();
  },
  /*导航切换*/
  clickNav:function(e) {
    this.setData({
      navId: e.currentTarget.dataset.id
    })
    this.setData({
      is_clickTab: 0,
      page: 1
    })
    this.getPendingOrdersData();
  },
  /*获取数据*/
  getPendingOrdersDataModel: new BaseModel({
    url: 'employees/bookingOrder',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getPendingOrdersData() {
    this.getPendingOrdersDataModel
        .fetch({
          page:this.data.page,
          size:this.data.size, //每页条数
          desc:this.data.navId //正序倒序
        })
        .then(res => {
          //成功
          if(this.data.is_clickTab==0){
            this.setData({
              pendingOrdersData:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else {
              //成功
              var pendingOrdersData_list = this.data.pendingOrdersData;
              for(var i=0;i<res.data.length;i++){
                pendingOrdersData_list.push(res.data[i])
              }
              this.setData({
                pendingOrdersData:pendingOrdersData_list
              })
            }
          }

        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  //获取上次记录
  getRecordDataModel:new BaseModel({
    url:"employees/getRecord",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getRecordData(){
    this.getRecordDataModel
        .fetch({
          verify:1
        })
        .then(res => {
          //成功 (1分总2普通员工)
          this.setData({
            navId: res.data.desc,//历程id
          })
          this.getPendingOrdersData();
        })
        .catch(res => {
        })
  },
  //获取角色
  getLevelDataModel:new BaseModel({
    url:"getLevel",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getLevelData(){
    this.getLevelDataModel
        .fetch({  })
        .then(res => {
          //成功 (1分总2普通员工)
          app.globalData.role=res.data;
          wx.setStorageSync('roleCode',res.data)
          this.setData({
            roleCode:res.data
          })
        })
        .catch(res => {
        })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getPendingOrdersData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    this.getPendingOrdersData();
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1
    })
    this.getPendingOrdersData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})